import 'dart:async';
import 'package:flutter_aliplayer/flutter_aliplayer.dart';
import 'package:flutter_aliplayer/flutter_aliplayer_factory.dart';
// import 'package:flutter_livepush_plugin/base/live_base.dart';
// import 'package:flutter_livepush_plugin/base/live_base_def.dart';
// import 'package:flutter_livepush_plugin/pusher/live_push_config.dart';
// import 'package:flutter_livepush_plugin/pusher/live_push_def.dart';
// import 'package:flutter_livepush_plugin/pusher/live_pusher.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/record/player_logic.dart';
import 'package:ball_lion/entity/live/live_detail_entity.dart';
import 'package:ball_lion/entity/live/expert_room_entity.dart';
import 'package:ball_lion/server/api/live_api.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/utils/image_utils.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/toast_utils.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/player/qs_player_logic.dart';
import 'package:ball_lion/widgets/player/qs_player.dart';
import 'package:ball_lion/widgets/base/wqs_dialog.dart';

import 'package:ball_lion/module/overlay/live_float_widget.dart';

class LiveNormalLogic extends GetxController {
  int? liveId = Get.arguments;
  LiveDetailEntity? liveDetail;
  late QsPlayerLogic playerLogic;
  Timer? _timer;
  bool _onPush = false;
  bool _mirror = false;
  bool _clickStart = false;
  int _count = 3;
  bool waitStart = false;
  bool hasInit = false;
  String? shareUrl;

  bool get onPush => _onPush;
  int get count => _count;
  bool get mirror => _mirror;
  bool get clickStart => _clickStart;

  set clickStart(bool value) {
    _clickStart = value;
    update();
  }

  set mirror(bool value) {
    _mirror = value;
    update();
  }

  set count(int value) {
    _count = value;
    update();
  }

  set onPush(bool value) {
    _onPush = value;
    update();
  }

  @override
  void onInit() async {
    liveDetail = await LiveApi.getLiveDetail(liveId);
    if (liveDetail?.status == null || liveDetail?.status == 0) waitStart = true;
    playerLogic = Get.put(QsPlayerLogic(), tag: "${Get.arguments}");
    playerLogic.url = liveDetail?.url ?? "";
    if (liveDetail?.landscapeScreen == 0) {
      playerLogic.setFit(false);
    }
    LiveFloatController.instance.prepare(liveDetail);
    hasInit = false;
    update();
    _timer = Timer.periodic(const Duration(seconds: 5), (timer) => getData());
    super.onInit();
  }

  Future getData() async {
    var data = await LiveApi.getLiveDetail(liveId);
    if (data?.hot.hasValue ?? false) {
      liveDetail?.hot = data?.hot;
    }
    if (waitStart && data?.status != null && data?.status != 0) {
      liveDetail = data;
      waitStart = false;
      playerLogic.doRefresh();
    }
    if (liveDetail?.isFocus != data?.isFocus) {
      liveDetail?.isFocus = data?.isFocus;
    }
    update();
  }

  Future getResource() async {
    var e = await OtherApi.getAppList("app_page_share");
    if (e != null) shareUrl = e.first.content;
    update();
  }

  Future exit() async {
    Utils.onEvent('spzb', params: {'spzb': 4});
    Get.back();
  }

  @override
  void onClose() {
    _timer?.cancel();
    playerLogic.dispose();
    super.onClose();
  }
}
